package cz.byteworks.android.myway.location;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.Timer;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.preference.PreferenceManager;
import cz.byteworks.android.myway.db.DataHelper;
import cz.byteworks.android.myway.pojo.Position;
import cz.byteworks.android.myway.util.NowButtonTextTask;

public class PositionService {
	
	private LocationManager _locationManager;
	private DataHelper _dataHelper;
	private Context _context;
	
	public PositionService(Context context, DataHelper dataHelper) {
		_locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		_dataHelper = dataHelper;
		_context = context;
	}
	
	public void storeActualPosition() {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_context);
		Position latestPosition = _dataHelper.getLatestPosition(_dataHelper.getActualWay());
		MyWayLocationListener locListener = new MyWayLocationListener(_context, _locationManager, _dataHelper, latestPosition);
		if (!prefs.getBoolean("useNetwork", false)) {
			_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4500, 2, locListener, _context.getMainLooper());
		}
		else {
			_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4500, 2, locListener, _context.getMainLooper());
			_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4500, 2, locListener, _context.getMainLooper());
		}
		AutoLocator.setLocationListener(locListener);
		AutoLocator.setLocating(true);
		scheduleButtonTextChange(_context);
	}
	
	public static String formatLat(double pCordinate) {
		DecimalFormat f = new DecimalFormat("00.00");
        int tDegree = (int) pCordinate;
        double tTmpMinutes = (Math.abs(pCordinate) - Math.abs(tDegree)) * 60;
        int tMinutes = Math.abs((int) tTmpMinutes);
        double tSeconds = Math.abs((tTmpMinutes - tMinutes) * 60);
        String l;
        if (pCordinate > 0) {
        	l = "N";
        }
        else {
        	l = "S";
        }
        return l+Math.abs(tDegree) + "\u00B0" + tMinutes + "\'" +f.format(tSeconds) + "\"";
	}
	
	public static String formatLon(double pCordinate) {
		DecimalFormat f = new DecimalFormat("00.00");
        int tDegree = (int) pCordinate;
        double tTmpMinutes = (Math.abs(pCordinate) - Math.abs(tDegree)) * 60;
        int tMinutes = Math.abs((int) tTmpMinutes);
        double tSeconds = Math.abs((tTmpMinutes - tMinutes) * 60);
        String l;
        if (pCordinate > 0) {
        	l = "E";
        }
        else {
        	l = "W";
        }
        return l+Math.abs(tDegree) + "\u00B0" + tMinutes + "\'" +f.format(tSeconds) + "\"";
	}
	
	private static void scheduleButtonTextChange(Context ctx) {
		Timer timer = new Timer();
		NowButtonTextTask task = new NowButtonTextTask(ctx, timer);
		timer.schedule(task, new Date(), 100);
	}
}
